package command;

import java.net.InetAddress;
import java.sql.Connection;
import java.util.ArrayList;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.*;
import command.Command;
import command.CommandException;
import db.CommonConnect;

import common.CommonUtil;
import common.Crypto;
import common.EDUBOX_Baese64_Crypto;
import common.UserSession;
import db.QueryManager;
import db.DAO;

import com.oreilly.servlet.MultipartRequest;
import common.SaveUploadFile;

/**
 * Command 인터페이스를 implement 하여 구현된 command.
 */
public class RaterInsertCommand extends Command {

	private String next;
	
	public RaterInsertCommand(String next) {
		this.next=next;
	}

	/**
	 * 실제 요청을 받아 처리하는 메소드
	 * 요청을 받으면  요청을 처리하고 next 페이지를 돌려줌
	 * 
	 * @param req HttpServletRequest
	 * @throws CommandException
	 * @return next 이동할 페이지
	 */
	public String execute(HttpServletRequest req)
	throws CommandException {
		ArrayList list =  null;
		HashMap listMap = new HashMap();	
	
		Connection con = null;  
		try {		
			String uploadpath=QueryManager.getQuery("upload_rater.path");
			MultipartRequest multi=SaveUploadFile.getMultipartRequest(req,uploadpath);
			
			List list_multi = SaveUploadFile.savefile(multi,uploadpath);
			HashMap file_info =null;
			String file_name="";
			String file_id="";
			String file_size="0";
			String originFileName = "";
			String fileInput = "";
			
			String file_name_1="";
			String file_id_1="";
			String file_size_1="0";
			String originFileName_1 = "";
			
			for ( int i=0;list_multi!=null && list_multi.size()>i;i++){
				file_info = (HashMap)list_multi.get(i);
				file_name=(String)file_info.get("fileName");
				file_id=(String)file_info.get("file_id");	
				file_size= (String)file_info.get("fileSize");
				originFileName= (String)file_info.get("originFileName");
				fileInput = (String)file_info.get("fileInput");
				
				//System.out.println("originFileName====>"+originFileName);
				//System.out.println("fileInput====>"+fileInput);
				
				if (file_name != null) {  
			    	  if (fileInput.equals("attachFile")){
			    		  file_name_1=(String)file_info.get("fileName");
						  file_id_1=(String)file_info.get("file_id");	
						  file_size_1= (String)file_info.get("fileSize");
						  originFileName_1= (String)file_info.get("originFileName");
			    	  }
			    	 
				}

			}
			
			String login_id = CommonUtil.nvl(Crypto.EncodeBySType(multi.getParameter("login_id")));
			String login_name = CommonUtil.nvl(multi.getParameter("login_name"));	
			
			String login_pw = CommonUtil.nvl(Crypto.EncodeBySType(multi.getParameter("login_pw")));	
			String addr = CommonUtil.nvl(multi.getParameter("addr"));	
			
			String email = CommonUtil.nvl(multi.getParameter("email"));	
			String tel_no = CommonUtil.nvl(multi.getParameter("tel_no"));	
			String bank_nm = CommonUtil.nvl(multi.getParameter("bank_nm"));
			String bank_no = CommonUtil.nvl(multi.getParameter("bank_no"));
			
			String photo_nm = originFileName_1;
			String photo_id = file_id_1;
			
			String state = CommonUtil.nvl(multi.getParameter("state"));
			String user_id = CommonUtil.nvl(multi.getParameter("user_id"));
						
						
			con = (new CommonConnect()).getConnection_ds2();
			con.setAutoCommit(false);
			String[] param=null;
			String queryStr = QueryManager.getQuery("rater_insert.query");
			param= new String[12];
			param[0] = login_id;	
			param[1] = login_name;	
			param[2] = login_pw;
			param[3] = addr;
			param[4] = email;
			param[5] = tel_no;
			param[6] = bank_nm;
			param[7] = bank_no;
			param[8] = photo_nm;
			param[9] = photo_id;
			param[10] = state;
			param[11] = user_id;
			
			list = DAO.select(con,queryStr,param);
			
			listMap.put("mainContent",list);
			logger.debug("queryStr ==> "+"rater_insert.query");		     
		     
// 리퀘스트에 모두 담는다.
			req.setAttribute("list_map",listMap);

		}catch (Exception ex) {
			try{
				con.rollback();
			}catch(Exception ee){}
			ex.printStackTrace();
			throw new CommandException(ex.getMessage());
		}finally {
			try{
				if (con!=null) con.setAutoCommit(true);
				if (con!=null) con.close();
			}catch(Exception ee){}
		}
		return next;
	}
	


}
